package com.kaim.calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9, btn_point,
            btn_clear, btn_delete, btn_plus, btn_minus, btn_multiply, btn_divide, btn_equle;
    EditText mEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        init();
    }

    private void init() {
        btn_0 = (Button) findViewById(R.id.btn_0);
        btn_1 = (Button) findViewById(R.id.btn_1);
        btn_2 = (Button) findViewById(R.id.btn_2);
        btn_3 = (Button) findViewById(R.id.btn_3);
        btn_4 = (Button) findViewById(R.id.btn_4);
        btn_5 = (Button) findViewById(R.id.btn_5);
        btn_6 = (Button) findViewById(R.id.btn_6);
        btn_7 = (Button) findViewById(R.id.btn_7);
        btn_8 = (Button) findViewById(R.id.btn_8);
        btn_9 = (Button) findViewById(R.id.btn_9);
        btn_point = (Button) findViewById(R.id.btn_point);
        btn_clear = (Button) findViewById(R.id.btn_clear);
        btn_delete = (Button) findViewById(R.id.btn_delete);
        btn_plus = (Button) findViewById(R.id.btn_plus);
        btn_minus = (Button) findViewById(R.id.btn_minus);
        btn_multiply = (Button) findViewById(R.id.btn_multiply);
        btn_divide = (Button) findViewById(R.id.btn_divide);
        btn_equle = (Button) findViewById(R.id.btn_equle);

        mEditText = (EditText) findViewById(R.id.edit_input);

        btn_0.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
        btn_plus.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_equle.setOnClickListener(this);
        btn_multiply.setOnClickListener(this);
        btn_minus.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String s = mEditText.getText().toString();
        switch (view.getId()) {
            case R.id.btn_0:
            case R.id.btn_1:
            case R.id.btn_2:
            case R.id.btn_3:
            case R.id.btn_4:
            case R.id.btn_5:
            case R.id.btn_6:
            case R.id.btn_7:
            case R.id.btn_8:
            case R.id.btn_9:
            case R.id.btn_point:
                mEditText.setText(String.format("%s%s", s, ((Button) view).getText()));
                break;
            case R.id.btn_plus:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                mEditText.setText(String.format("%s %s ", s, ((Button) view).getText()));
                break;
            case R.id.btn_clear:
                mEditText.setText("");
                break;
            case R.id.btn_delete:
                if (!s.equals("")) {
                    mEditText.setText(s.substring(0, s.length() - 1));
                }
                break;
            case R.id.btn_equle:
                getResult();
                break;
        }
    }

    /**
     * 运算结果
     */
    private void getResult() {
        String s = mEditText.getText().toString();
        double r = 0;
        int space = s.indexOf(' ');//这里使用单引号,用于搜索空格位置
        String s1 = s.substring(0, space);//s1用于保存第一个运算数
        String op = s.substring(space + 1, space + 2);//op用于保存运算符
        String s2 = s.substring(space + 3);//s2用于保存第二个运算数
        double arg1 = Double.parseDouble(s1);
        double arg2 = Double.parseDouble(s2);

        switch (op) {
            case "+":
                r = arg1 + arg2;
                break;
            case "—":
                r = arg1 - arg2;
                break;
            case "×":
                r = arg1 * arg2;
                break;
            case "/":
                r = arg1 / arg2;
                break;
        }
        mEditText.setText(String.valueOf(r));
    }
}
